package org.domon.manager.base.handler;

import org.domon.manager.common.WebConstants;
import org.domon.manager.sys.model.UserInfo;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;

public class WebArgumentResolverHandler implements WebArgumentResolver {

	/**
	 * 手动注入controller参数 注入controller参数列表里的UserInfo
	 */
	@Override
	public Object resolveArgument(MethodParameter methodParameter,
			NativeWebRequest webRequest) throws Exception {
		// 第一个参数类型是UserInfo
		if (UserInfo.class.isAssignableFrom(methodParameter.getParameterType())
				&& methodParameter.getParameterIndex() == 0) {
			return webRequest.getAttribute(WebConstants.USER,
					RequestAttributes.SCOPE_SESSION);
		}
		return UNRESOLVED;
	}
}
